/*
 * USARTLogging.cpp
 *
 *  Created on: 12.12.2012
 *      Author: Lapchinskiy
 */
#include "BuildConfig.h"
#include "Global_types.h"
#include "TimeManager.h"
#include "SerialInterface.h"
#include <iostream>
#include <stdio.h>
#include "Logging.h"
volatile bool globalLogEnable = true;
char const *ModuleToStringTable[] = { "Task", "Driver", "UART", "Sensor",
		"Scheduler", "" };
char const *ErrorToStringTable[] = { "No error", "FAILURE", "" };
void Log(char *string) {
#ifdef USART_DEBUG
	TimeManager::enabled = FALSE;
	if (globalLogEnable == true) {
		SerialPutString("-> Time: ");
		SerialPutIntegerAsString(TimeManager::systemTime);
		SerialPutString(" ");
		SerialPutString(string);
		SerialPutString("\r\n");
	}
	TimeManager::enabled = TRUE;

	//uint8_t* tmpStr;
	//	SerialPutString("-> Time: ");
	//	SerialPutIntegerAsString(TimeManager::systemTime);
	//(": ");

#endif //USART_DEBUG
}
void PutNumberToTerminal(uint32_t number) {
#ifdef USART_DEBUG
	TimeManager::enabled = FALSE;
	SerialPutIntegerAsString(number);
	// SerialPutString(" \n");
	//	SerialPutString("-> Time: ");
	//	SerialPutIntegerAsString(TimeManager::systemTime);
	//(": ");
	TimeManager::enabled = TRUE;
#endif //USART_DEBUG
}

void PutStringToTerminal(char *string) {
#ifdef USART_DEBUG
	TimeManager::enabled = FALSE;
	SerialPutString(string);
// SerialPutString(" \n");
//	SerialPutString("-> Time: ");
//	SerialPutIntegerAsString(TimeManager::systemTime);
//(": ");
	TimeManager::enabled = TRUE;
#endif //USART_DEBUG
}
void MultiPutStringToTerminal(char *string, uint8_t times) {
	for (uint8_t i = 0; i < times; i++) {
		PutStringToTerminal(string);
	}
}
void Log(ModuleType module, ErrorCode error, char *string) {

#ifdef USART_DEBUG
	TimeManager::enabled = FALSE;
	if (globalLogEnable == true) {
		SerialPutString("-> Time: ");
		SerialPutIntegerAsString(TimeManager::systemTime);
		SerialPutString(" ");
		SerialPutString("Module: ");
		SerialPutString(ModuleToStringTable[module]);
		SerialPutString(" ");
		SerialPutString(string);
		SerialPutString(" ");
		SerialPutString("Error: ");
		SerialPutString(ErrorToStringTable[error]);
		//    SerialPutString("\r");
		SerialPutString("\r\n");
	}

	TimeManager::enabled = TRUE;

#endif //USART_DEBUG
}

void Log(LogRecord *logrec, ErrorCode error, char *string) {
#ifdef USART_DEBUG
	TimeManager::enabled = FALSE;

	if ((logrec->logEnable == true) && (globalLogEnable == true)) {
		SerialPutString("-> Time: ");
		SerialPutIntegerAsString(TimeManager::systemTime);
		SerialPutString(" ");
		SerialPutString("Module: ");
		SerialPutString(ModuleToStringTable[logrec->module]);
		SerialPutString(" ");
		SerialPutString("\"");
		SerialPutString(logrec->objectName);
		SerialPutString("\" ");
		SerialPutString(string);
		SerialPutString(" ");
		SerialPutString("Error: ");
		SerialPutString(ErrorToStringTable[error]);
		SerialPutString("\r\n");
	}
	TimeManager::enabled = TRUE;
#endif //USART_DEBUG
}
void LoggingEnable(bool enable) {
	globalLogEnable = enable;
}

